package com.cloudspace.jindun.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudspace.jindun.JindunApp;
import com.cloudspace.jindun.R;
import com.cloudspace.jindun.model.ChannelInfo;
import com.cloudspace.jindun.net.manager.ImageManager;
import com.cloudspace.jindun.utils.ObjUtil;

import java.util.List;

/**
 * Created by zengxianhua on 17/1/25.
 */

public class MyChannelAdapter extends BaseAdapter{
    private List<ChannelInfo> datas;

    public void setDatas(List<ChannelInfo> data){
        this.datas = data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (ObjUtil.isEmpty(datas)){
            return 0;
        }
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        if (getCount() == 0 || position >= getCount() || position < 0){
            return null;
        }
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder = null;
        if (convertView == null){
            convertView = LayoutInflater.from(JindunApp.getInstance()).inflate(
                    R.layout.item_my_channel, null);
            holder = new Holder();
            holder.channelIdTv = (TextView) convertView.findViewById(R.id.channelId);
            holder.channelNameTv = (TextView) convertView.findViewById(R.id.channelName);
            holder.channelIconImg = (ImageView) convertView.findViewById(R.id.channelIcon);
            holder.channelPersonNumberTv = (TextView) convertView.findViewById(R.id.channelPersonNumber);
            holder.channelCreateTimeTv = (TextView) convertView.findViewById(R.id.channelCreateDate);
            convertView.setTag(holder);
        }

        holder = (Holder) convertView.getTag();

        ChannelInfo info = datas.get(position);
        holder.channelIdTv.setText(String.valueOf(info.channelId));
        holder.channelNameTv.setText(String.valueOf(info.channelName));
        ImageManager.loadCircleImage(JindunApp.getInstance(), holder.channelIconImg, info.iconUrl);
        holder.channelPersonNumberTv.setText(String.valueOf(info.totalPer));
        holder.channelCreateTimeTv.setText(info.chaCreDateValue);
        return convertView;
    }

    static class Holder{
        public TextView channelNameTv;
        public TextView channelIdTv;
        public ImageView channelIconImg;
        public TextView channelCreateTimeTv;
        public TextView channelPersonNumberTv;
    }
}
